<?php
namespace Controller;

require_once 'Controller.php';
require_once 'model/Attribut.php';

use Controller\Controller;
use Model\Attribut;
use Classes\Paginator;
use Classes\Field;

class AttributController extends Controller {
    public function __construct() {
        $this->name = 'attribut';
    }
    
    public function indexView() {
        $this->headerTitle = 'Gestion des attributs';        
        $attributObject = new Attribut();
        $items_total = $attributObject->getTotalCount()->total;
        $paginator = new Paginator($items_total, 10);
        $paginator->page_nav_class = "btn btn-xs btn-default";
        $paginator->page_nav_desabled_class = "btn btn-xs btn-primary";
        
        $variables = array(            
            'attributs' => $attributObject->fetchAll(null, null, null, $paginator->limit_start, $paginator->items_per_page),
            'pager' => $paginator->pager(),
            'start_num' => $paginator->start_num,
            'beginForm' => $paginator->beginForm(),
            'endForm' => $paginator->endForm()
        );
        $this->displayView('index', $variables);
    }
    
    public function addView() {
        $this->headerTitle = 'Ajout d\'un attribut';
        $field = new Field();
        $attributObject = new Attribut();
        
        if($this->getPost()) {
            $post = $this->getPost();
            $data = $post['data'];
            $data['alias'] = \Functions\makeSafeString($data['alias']);
            $data['position'] += 1; 
            if($attributObject->saveAttribut($data)) {
                $this->redirect($this->name);
            }
        }
        
        $variables = array(
            'fieldDefaultValues' => $field->getFieldDefaultValues(),
            'fieldAffectedValues' => $field->getFieldAffectedValues(),
            'fieldTypes' => $field->getFieldTypes(),
            'attributs' => $attributObject->fetchAll()
        );
        $this->displayView('add', $variables);
    }
    
    public function editView() {
        $this->headerTitle = 'Edition d\'un attribut';
        $field = new Field();
        $attributObject = new Attribut();
        $id_attribut = (int) $_GET['id'];
        
        if($this->getPost()) {
            $post = $this->getPost();
            $data = $post['data'];
            $data['alias'] = \Functions\makeSafeString($data['alias']);
            $data['position'] += 1; 
            if($attributObject->saveAttribut($data)) {
                $this->redirect($this->name);
            }
        }
        
        $variables = array(
            'fieldDefaultValues' => $field->getFieldDefaultValues($attributObject->getAttribut($id_attribut)->default_value),
            'fieldAffectedValues' => $field->getFieldAffectedValues($attributObject->getAttribut($id_attribut)->affected_value),
            'fieldTypes' => $field->getFieldTypes($attributObject->getAttribut($id_attribut)->type),
            'attribut' => $attributObject->getAttribut($id_attribut),
            'allAttributs' => $attributObject->fetchAll()
        );
        $this->displayView('edit', $variables);
    }
    
    public function deleteView() {
        $id_attribut = (int) $_GET['id'];
        $attributObject = new Attribut();
        if($attributObject->deleteAttribut($id_attribut)) {
            $this->redirect($this->name);
        }
    }
}